﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLKTX1._0.DAO;
using QLKTX1._0.DTO;

namespace QLKTX1._0.GUI.CauHinh
{
    public partial class FrmLoaiChiPhi : Form
    {
        public FrmLoaiChiPhi()
        {
            InitializeComponent();
        }

        private void FrmLoaiChiPhi_Load(object sender, EventArgs e)
        {
            btnctkgdnLuu.Enabled = false;
            //--------------LOAD DATAGRIDVIEW LOAI CHI PHI-------------------------------------
            dtgvLoaiChiPhi.DataSource = LoaiChiPhiDAO.dsLoaiChiPhi();
            //doi ten cot sang tieng viet--------------------------
            dtgvLoaiChiPhi.Columns[0].HeaderText = "Mã loại chi phí";
            dtgvLoaiChiPhi.Columns[1].HeaderText = "Tên loại chi phí";
            dtgvLoaiChiPhi.Columns[2].HeaderText = "Đơn vị tính"; 
            //Chinh do rong cua cot..............
            dtgvLoaiChiPhi.Columns[0].Width = 150;
            dtgvLoaiChiPhi.Columns[1].Width = 150;
            dtgvLoaiChiPhi.Columns[2].Width = 150;
            //Cam chinh sua cot, chi doc.............
            dtgvLoaiChiPhi.Columns[0].ReadOnly = true;
            dtgvLoaiChiPhi.Columns[1].ReadOnly = true;
            dtgvLoaiChiPhi.Columns[2].ReadOnly = true;

            //gan du lieu gridview len textbox-------------------------------
            txtMLCP.DataBindings.Clear();
            txtMLCP.DataBindings.Add("Text", dtgvLoaiChiPhi.DataSource, "maLoaiChiPhi");
            txtTLCP.DataBindings.Clear();
            txtTLCP.DataBindings.Add("Text", dtgvLoaiChiPhi.DataSource, "tenLoaiChiPhi");
            txtDVT.DataBindings.Clear();
            txtDVT.DataBindings.Add("Text", dtgvLoaiChiPhi.DataSource, "donViTinh");
            //------------an cac nut----------------------
            btnlcpLuu.Enabled = false;
            lbTSDMLCP.Text = "Tổng số danh mục hiện có là: " + LoaiChiPhiDAO.dsLoaiChiPhi().Count.ToString();


            //--------------LOAD DATAGRIDVIEW KHUNG GIA DIEN NUOC-------------------------------------
            dtgvKhungGiaDienNuoc.DataSource = KhungGia_DienNuocDAO.dsKhungGia_DienNuoc();
            //gan du lieu gridview len textbox-------------------------------
            txtkgdnMKG.DataBindings.Clear();
            txtkgdnMKG.DataBindings.Add("Text", dtgvKhungGiaDienNuoc.DataSource, "maKhungGiaDienNuoc");
            txtkgdnTKG.DataBindings.Clear();
            txtkgdnTKG.DataBindings.Add("Text", dtgvKhungGiaDienNuoc.DataSource, "moTa");
            //doi ten cot sang tieng viet--------------------------
            dtgvKhungGiaDienNuoc.Columns[0].HeaderText = "Mã khung giá";
            dtgvKhungGiaDienNuoc.Columns[1].HeaderText = "Tên khung giá";
            //Chinh do rong cua cot..............
            dtgvKhungGiaDienNuoc.Columns[0].Width = 165;
            dtgvKhungGiaDienNuoc.Columns[1].Width = 265;
            //Cam chinh sua cot, chi doc.............
            dtgvKhungGiaDienNuoc.Columns[0].ReadOnly = true;
            dtgvKhungGiaDienNuoc.Columns[1].ReadOnly = true;
            //------------an cac nut----------------------
            btnkgdnLuu.Enabled = false;
            lbKGDN.Text = "Tổng số danh mục hiện co: "+KhungGia_DienNuocDAO .dsKhungGia_DienNuoc ().Count .ToString ();


            //--------------LOAD DATAGRIDVIEW CHI TIET KHUNG GIA DIEN NUOC-------------------------------------

            cmbTenLoaiChiPhi.DataSource =LoaiChiPhiDAO.dsLoaiChiPhi();
            cmbTenLoaiChiPhi.DisplayMember = "tenLoaiChiPhi";
           cmbTenLoaiChiPhi.ValueMember = "maLoaiChiPhi";

            cmbTenKhungGia.DataSource = KhungGia_DienNuocDAO.dsKhungGia_DienNuoc();
            cmbTenKhungGia.DisplayMember = "moTa";
            cmbTenKhungGia.ValueMember = "maKhungGiaDienNuoc";

            //-------------****************************************-------------------------
            dtgvCTKhungGiaDN.DataSource = this.xemChiTietKhungGiaDN();            

            // thay doi ten cot
            dtgvCTKhungGiaDN.Columns[0].HeaderText = "Loại chi phí";
            dtgvCTKhungGiaDN.Columns[1].HeaderText = "Tên khung giá";
            dtgvCTKhungGiaDN.Columns[2].HeaderText = "Giá thành";
            //Chinh do rong cua cot..............
            dtgvCTKhungGiaDN.Columns[0].Width = 165;
            dtgvCTKhungGiaDN.Columns[1].Width = 165;
            dtgvCTKhungGiaDN.Columns[2].Width = 105;
            //Cam chinh sua cot, chi doc.............
            dtgvCTKhungGiaDN.Columns[0].ReadOnly = true;
            dtgvCTKhungGiaDN.Columns[1].ReadOnly = true;
            dtgvCTKhungGiaDN.Columns[2].ReadOnly = true;
            //gan du lieu gridview len textbox-------------------------------
            cmbTenLoaiChiPhi.DataBindings.Clear();
            cmbTenLoaiChiPhi.DataBindings.Add("Text", dtgvCTKhungGiaDN.DataSource, "tenLCP");

            cmbTenKhungGia.DataBindings.Clear();
            cmbTenKhungGia.DataBindings.Add("Text", dtgvCTKhungGiaDN.DataSource, "tenKhungGia");

            txtGiaThanh.DataBindings.Clear();
            txtGiaThanh.DataBindings.Add("Text", dtgvCTKhungGiaDN.DataSource, "giaThanh");

            lbCTKGDN.Text = "Tổng số danh mục chi tiết điện nước có là: " +ChiTietKhungGiaDienNuocDAO .dsChiTietKhungGiaDienNuoc ().Count .ToString ();
        }

        //-----------Dung Datatable hien thi chi tiet khung gia dien nuoc----------
        private DataTable xemChiTietKhungGiaDN()
        {
            DataTable dt = new DataTable(); // bang de chua
           

            DataColumn col1=new DataColumn("tenLCP");
           
            col1.DataType = System.Type.GetType("System.String");
            dt.Columns.Add(col1);
            DataColumn col2 = new DataColumn("tenKhungGia");
            col2.DataType = System.Type.GetType("System.String");
            dt.Columns.Add(col2);
            DataColumn col3 = new DataColumn("giaThanh");
            col3.DataType = System.Type.GetType("System.Int32");
            
            dt.Columns.Add(col3);

            // dt.Columns.Add(new DataColumn("Tên Loại Chi Phí"));
             //dt.Columns.Add(new DataColumn("Tên Loại Chi Phí"));
            // dt.Columns.Add(new DataColumn("Tên Loại Chi Phí"));

            foreach (ChiTietKhungGiaDienNuocDTO dot in ChiTietKhungGiaDienNuocDAO.dsChiTietKhungGiaDienNuoc())
            {
                dt.AcceptChanges();

                DataRow dr = dt.NewRow(); // tao 1 hang

                dr[0] = dot.MaLoaiChiPhi.TenLoaiChiPhi;
                dr[1] = dot.MaKhungGiaDienNuoc.MoTa;

                dr[2] = dot.GiaThanh;        

                dt.Rows.Add(dr);

            } 

            return dt;
        }
        //--------------------------------Loai chi phi-------------------
        private void btnlcpThem_Click(object sender, EventArgs e)
        {
            if (btnlcpThem.Text.Equals("Thêm"))
            {
                btnlcpCapNhat.Enabled = false;
                btnlcpXoa.Enabled = false;
                btnlcpLuu.Enabled = true;
                txtMLCP.Text = Service.generateNoAuto("loaiChiPhi", "maLoai_chiPhi", "L", 1);
                //txtMLCP.Text = "";
                txtTLCP.Text = "";
                txtDVT.Text = "";
                btnlcpThem.Text = "Hủy bỏ";

            }
            else
            {
                btnlcpThem.Text = "Thêm";
                btnlcpCapNhat.Enabled = true ;
                btnlcpXoa.Enabled = true;
                btnlcpLuu.Enabled = false;
                this.FrmLoaiChiPhi_Load(sender, e);
            }
        }

        private void btnlcpLuu_Click(object sender, EventArgs e)
        {
            if (btnlcpThem.Text.Equals("Hủy bỏ"))
            {
                if (txtTLCP.Text.Equals("") || txtDVT .Text .Equals (""))
                {
                    MessageBox.Show("Vui lòng nhập dữ liệu", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    LoaiChiPhiDTO lcp = new LoaiChiPhiDTO();
                    lcp.MaLoaiChiPhi = txtMLCP.Text;
                    lcp.TenLoaiChiPhi = txtTLCP.Text;
                    lcp.DonViTinh = txtDVT.Text;

                    LoaiChiPhiDAO.themLoaiChiPhi(lcp);
                                        
                    btnlcpThem.Text = "Thêm";
                    btnlcpCapNhat.Enabled = true;
                    btnlcpXoa.Enabled = true;
                    btnlcpThem.Enabled = true;
                    this.FrmLoaiChiPhi_Load(sender, e);
                }
            }
            else
            {
                if (txtTLCP.Text.Equals("") || txtDVT.Text.Equals(""))
                {
                    MessageBox.Show("Vui lòng nhập dữ liệu", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    LoaiChiPhiDTO lcp = new LoaiChiPhiDTO();
                    lcp.MaLoaiChiPhi = txtMLCP.Text;
                    lcp.TenLoaiChiPhi = txtTLCP.Text;
                    lcp.DonViTinh = txtDVT.Text;

                    LoaiChiPhiDAO.capNhatLoaiChiPhi(lcp);
                 
                    btnlcpCapNhat.Text = "Cập Nhật";
                    btnlcpCapNhat.Enabled = true;
                    btnlcpXoa.Enabled = true;
                    btnlcpThem.Enabled = true;
                    this.FrmLoaiChiPhi_Load(sender, e);
                }
            }
        }

        private void btnlcpCapNhat_Click(object sender, EventArgs e)
        {
            if(btnlcpCapNhat.Text .Equals ("Cập Nhật") )
            {
                btnlcpLuu.Enabled = true;
                btnlcpThem.Enabled = false;
                btnlcpXoa.Enabled = false;              
                btnlcpCapNhat.Text = "Hủy bỏ";
               // this.FrmLoaiChiPhi_Load(sender, e);

            }
            else
            {
                btnlcpLuu.Enabled = false ;
                btnlcpThem.Enabled = true ;
                btnlcpXoa.Enabled = true ;
               
                btnlcpCapNhat.Text = "Cập Nhật";
                this.FrmLoaiChiPhi_Load(sender, e);
            }
        }

        private void btnlcpXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn xóa ?", "Xác nhận xóa ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                LoaiChiPhiDTO lcp = new LoaiChiPhiDTO();
                lcp.MaLoaiChiPhi = txtMLCP.Text;
                lcp.TenLoaiChiPhi = txtTLCP.Text;
                lcp.DonViTinh = txtDVT.Text;

                LoaiChiPhiDAO.xoaLoaiChiPhi(lcp);

                this.FrmLoaiChiPhi_Load(sender, e);
            }
        }

        private void btnkgdnThem_Click(object sender, EventArgs e)
        {
            if (btnkgdnThem.Text.Equals("Thêm"))
            {
                btnkgdnCapNhat.Enabled = false;
                btnkgdnXoa.Enabled = false;
                btnkgdnLuu.Enabled = true;
              
                txtkgdnMKG.Text = Service.generateNoAuto("khungGia_dienNuoc", "maKhungGia_dienNuoc", "K", 1);
                //txtMLCP.Text = "";
                txtkgdnTKG.Text = "";

                btnkgdnThem.Text = "Hủy bỏ";

            }
            else
            {
                btnkgdnThem.Text = "Thêm";
                btnkgdnCapNhat.Enabled = true;
                btnkgdnXoa.Enabled = true;
                btnkgdnLuu.Enabled = false;
               // this.FrmLoaiChiPhi_Load(sender, e);
            }
        }

        private void btnkgdnLuu_Click(object sender, EventArgs e)
        {
            if (btnkgdnThem.Text.Equals("Hủy bỏ"))
            {
                if (txtkgdnTKG.Text.Equals(""))
                {
                    MessageBox.Show("Vui lòng nhập dữ liệu", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    
                    KhungGiaDienNuocDTO kgdn = new KhungGiaDienNuocDTO();
                    kgdn.MaKhungGiaDienNuoc = txtkgdnMKG.Text;
                    kgdn.MoTa = txtkgdnTKG.Text;

                    KhungGia_DienNuocDAO.themKhungGiaDienNuoc(kgdn);

                    btnkgdnThem.Text = "Thêm";
                    btnkgdnCapNhat.Enabled = true;
                    btnkgdnXoa.Enabled = true;
                    btnkgdnThem.Enabled = true;
                    this.FrmLoaiChiPhi_Load(sender, e);
                }
            }
            else
            {
                if (txtkgdnTKG.Text.Equals(""))
                {
                    MessageBox.Show("Vui lòng nhập dữ liệu", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    KhungGiaDienNuocDTO kgdn = new KhungGiaDienNuocDTO();
                    kgdn.MaKhungGiaDienNuoc = txtkgdnMKG.Text;
                    kgdn.MoTa = txtkgdnTKG.Text;

                    KhungGia_DienNuocDAO.capNhatKhungGiaDienNuoc(kgdn);

                    btnkgdnCapNhat.Text = "Cập Nhật";
                    btnkgdnCapNhat.Enabled = true;
                    btnkgdnXoa.Enabled = true;
                    btnkgdnThem.Enabled = true;
                    this.FrmLoaiChiPhi_Load(sender, e);
                }
            }
        }

        private void btnkgdnCapNhat_Click(object sender, EventArgs e)
        {
            if (btnkgdnCapNhat.Text.Equals("Cập Nhật"))
            {
                btnkgdnLuu.Enabled = true;
                btnkgdnThem.Enabled = false;
                btnkgdnXoa.Enabled = false;
                btnkgdnCapNhat.Text = "Hủy bỏ";
                // this.FrmLoaiChiPhi_Load(sender, e);

            }
            else
            {
                btnkgdnLuu.Enabled = false;
                btnkgdnThem.Enabled = true;
                btnkgdnXoa.Enabled = true;

                btnkgdnCapNhat.Text = "Cập Nhật";
                this.FrmLoaiChiPhi_Load(sender, e);
            }
        }

        private void btnkgdnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn xóa ?", "Xác nhận xóa ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                KhungGiaDienNuocDTO kgdn = new KhungGiaDienNuocDTO();
                kgdn.MaKhungGiaDienNuoc = txtkgdnMKG.Text;
                kgdn.MoTa = txtkgdnTKG.Text;

                KhungGia_DienNuocDAO.xoaKhungGiaDienNuoc(kgdn);

                this.FrmLoaiChiPhi_Load(sender, e);
            }
        }
//-----------------------------------------------------------------------------------------------------------
        private void btnctkgdnThem_Click(object sender, EventArgs e)
        {
            if (btnctkgdnThem.Text.Equals("Thêm"))
            {
                btnctkgdnCapNhat.Enabled = false;
                btnctkgdnXoa.Enabled = false;
                btnctkgdnLuu.Enabled = true;

               // txtkgdnMKG.Text = Service.generateNoAuto("khungGia_dienNuoc", "maKhungGia_dienNuoc", "K", 1);
                //txtMLCP.Text = "";
                txtGiaThanh.Text = "";

                btnctkgdnThem.Text = "Hủy bỏ";

            }
            else
            {
                btnctkgdnThem.Text = "Thêm";
                btnctkgdnCapNhat.Enabled = true;
                btnctkgdnXoa.Enabled = true;
                btnctkgdnLuu.Enabled = false;
                // this.FrmLoaiChiPhi_Load(sender, e);
            }
        }
        //------------------------------------------------------------------
        private void btnctkgdnLuu_Click(object sender, EventArgs e)
        {
            if (btnctkgdnThem.Text.Equals("Hủy bỏ"))
            {
                if (txtGiaThanh.Text.Equals(""))
                {
                    MessageBox.Show("Vui lòng nhập dữ liệu", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    ChiTietKhungGiaDienNuocDTO ct = new ChiTietKhungGiaDienNuocDTO();
                    ct.GiaThanh = Int32.Parse (txtGiaThanh.Text);


                   
                    string kgdn = cmbTenKhungGia.SelectedValue.ToString();
                    KhungGiaDienNuocDTO kg = new KhungGiaDienNuocDTO();
                    kg.MaKhungGiaDienNuoc = kgdn;
                    ct.MaKhungGiaDienNuoc = kg;

                    string lcp = cmbTenLoaiChiPhi.SelectedValue.ToString();
                    LoaiChiPhiDTO cp = new LoaiChiPhiDTO();
                    cp.MaLoaiChiPhi = lcp;
                    ct.MaLoaiChiPhi = cp;
                    if (ValidateData.checkDataExistabled("chiTiet_khungGia_dienNuoc","maKhungGia_dienNuoc",kgdn) && ValidateData.checkDataExistabled("chiTiet_khungGia_dienNuoc","maLoai_chiPhi",lcp)){

                        FrmThongBao f = new FrmThongBao("Da co roi");

                        f.StartPosition = FormStartPosition.CenterScreen;

                        f.TopLevel = true;

                        f.ShowDialog();

                    } else  ChiTietKhungGiaDienNuocDAO.themChiTietKhungGiaDienNuoc(ct );

                    btnctkgdnThem.Text = "Thêm";
                    btnctkgdnCapNhat.Enabled = true;
                    btnctkgdnXoa.Enabled = true;
                    btnctkgdnThem.Enabled = true;
                    this.FrmLoaiChiPhi_Load(sender, e);
                }
            }
            else
            {
                if (txtkgdnTKG.Text.Equals(""))
                {
                    MessageBox.Show("Vui lòng nhập dữ liệu", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    ChiTietKhungGiaDienNuocDTO ct = new ChiTietKhungGiaDienNuocDTO();
                    ct.GiaThanh = Int32.Parse(txtGiaThanh.Text);



                    string kgdn = cmbTenKhungGia.SelectedValue.ToString();
                    KhungGiaDienNuocDTO kg = new KhungGiaDienNuocDTO();
                    kg.MaKhungGiaDienNuoc = kgdn;
                    ct.MaKhungGiaDienNuoc = kg;

                    string lcp = cmbTenLoaiChiPhi.SelectedValue.ToString();
                    LoaiChiPhiDTO cp = new LoaiChiPhiDTO();
                    cp.MaLoaiChiPhi = lcp;
                    ct.MaLoaiChiPhi = cp;

                    ChiTietKhungGiaDienNuocDAO.capNhatChiTietKhungGiaDienNuoc(ct);

                    btnctkgdnCapNhat.Text = "Cập Nhật";
                    btnctkgdnCapNhat.Enabled = true;
                    btnctkgdnXoa.Enabled = true;
                    btnctkgdnThem.Enabled = true;
                    this.FrmLoaiChiPhi_Load(sender, e);
                }
            }
        }

        private void lbTSDMLCP_Click(object sender, EventArgs e)
        {
            
        }

        private void btnctkgdnCapNhat_Click(object sender, EventArgs e)
        {
            if (btnctkgdnCapNhat.Text.Equals("Cập Nhật"))
            {
                btnctkgdnLuu.Enabled = true;
                btnctkgdnThem.Enabled = false;
                btnctkgdnXoa.Enabled = false;
                btnctkgdnCapNhat.Text = "Hủy bỏ";
                // this.FrmLoaiChiPhi_Load(sender, e);

            }
            else
            {
                btnctkgdnLuu.Enabled = false;
                btnctkgdnThem.Enabled = true;
                btnctkgdnXoa.Enabled = true;

                btnctkgdnCapNhat.Text = "Cập Nhật";
                this.FrmLoaiChiPhi_Load(sender, e);
            }
        }

        private void btnctkgdnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn xóa ?", "Xác nhận xóa ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                ChiTietKhungGiaDienNuocDTO ct = new ChiTietKhungGiaDienNuocDTO();
               // ct.GiaThanh = Int32.Parse(txtGiaThanh.Text);



                string kgdn = cmbTenKhungGia.SelectedValue.ToString();
                KhungGiaDienNuocDTO kg = new KhungGiaDienNuocDTO();
                kg.MaKhungGiaDienNuoc = kgdn;
                ct.MaKhungGiaDienNuoc = kg;

                string lcp = cmbTenLoaiChiPhi.SelectedValue.ToString();
                LoaiChiPhiDTO cp = new LoaiChiPhiDTO();
                cp.MaLoaiChiPhi = lcp;
                ct.MaLoaiChiPhi = cp;

                
                ChiTietKhungGiaDienNuocDAO.xoaChiTietKhungGiaDienNuoc(ct);
                this.FrmLoaiChiPhi_Load(sender, e);
            }
        }
    }
}
